//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <aura.h>

Aura_Bool AuraTest_Test4()
{
    Aura_Address virAddr;
    Aura_MemorySize size = 0x3000;
    Aura_Bool bIsValid = AURA_FALSE;
    Aura_Address BaseAddr;
    Aura_MemorySize querySize;

    Aura_ECode ec = AuraVirtualMemory_Map(0x48010000,
                                    size,
                                    Aura_MemoryMap_Fixed | Aura_MemoryMap_Downgrow,
                                    Aura_MemoryProtection_Write,
                                    &virAddr);
    if (AURA_FAILED(ec)) {
       Aura_printf(" <AuraVirtualMemory_Map> failed. (ec = 0x%x).\n", ec);
       goto ErrorExit;
        return 1;
    }

    ec =AuraVirtualMemory_CheckProtection(virAddr,
                                        Aura_MemoryProtection_Write,
                                        &bIsValid);
    if (AURA_FAILED(ec) || !bIsValid) {
        Aura_printf(" <AuraVirtualMemory_CheckProtection> failed. (ec = 0x%x).\n",ec);
        goto ErrorExit;
    }

    ec = AuraVirtualMemory_SetProtection(virAddr, size, Aura_MemoryProtection_Read);
    if (AURA_FAILED(ec)) {
        Aura_printf(" <AuraVirtualMemory_SetProtection> failed. (ec = 0x%x).\n",ec);
        goto ErrorExit;
    }

    ec = AuraVirtualMemory_CheckProtection(virAddr-0x1000,
                                        Aura_MemoryProtection_Read,
                                        &bIsValid);
    if (AURA_FAILED(ec) || !bIsValid) {
        Aura_printf(" <AuraVirtualMemory_CheckProtection> failed. (ec = 0x%x).\n",ec);
        goto ErrorExit;
    }

    ec = AuraVirtualMemory_Commit(virAddr-0x1000);
    if (AURA_FAILED(ec)) {
         Aura_printf(" <AuraVirtualMemory_Commit> failed. (ec = 0x%x).\n",ec);
        goto ErrorExit;
    }

    ec = AuraVirtualMemory_Query(virAddr, &BaseAddr, &querySize);
    if (AURA_FAILED(ec)) {
         Aura_printf(" <AuraVirtualMemory_Query> failed. (ec = 0x%x).\n",ec);
    }
    else {
        if (querySize != size) {
            Aura_printf(" <the size is not matched. (querySize: 0x%x, size: 0x%x).\n",
                  querySize, size);
        }
    }

    ec = AuraVirtualMemory_Unmap(virAddr, size);
    if (AURA_FAILED(ec)) {
         Aura_printf(" <AuraVirtualMemory_Unmap> failed. (ec = 0x%x).\n",ec);
        return 1;
    }

    return AURA_TRUE;

ErrorExit:
    AuraVirtualMemory_Unmap(virAddr, size);
    return AURA_FALSE;
}

